local pingrong = fk.CreateSkill {
  name = "pingrong",
  
}


pingrong:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Finish and
    player:usedSkillTimes(self.name, Player.HistoryRound) == 0 and 
    table.find(player.room.alive_players, function (p)
      return p:getMark("@@caocao_lie") > 0
    end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return p:getMark("@@caocao_lie") > 0
    end)
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#pingrong-choose", self.name, true)
    if #to > 0 then
      event:setCostData(self,{tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self).tos[1])
    room:setPlayerMark(to, "@@caocao_lie", 0)
    room:addPlayerMark(player, "@@pingrong_extra", 1)
    if player == target then
      room:setPlayerMark(player, "pingrong_self-turn", 1)
    end
    player:gainAnExtraTurn()
  end,
})

pingrong:addEffect(fk.TurnedOver, {
  mute=true,
  can_refresh = function(self, event, target, player, data)
    --FIXME:巨大隐患
    if player ~= target or player:getMark("@@pingrong_extra") == 0 then return false end
    local e = player.room.logic:getCurrentEvent()
    return e.parent == nil or e:findParent(GameEvent.Turn, true) == nil
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@pingrong_extra", 0)
  end,
})
pingrong:addEffect(fk.AfterTurnEnd, {
  mute=true,
  can_refresh = function(self, event, target, player, data)
    --FIXME:巨大隐患
    if player ~= target or player:getMark("@@pingrong_extra") == 0 then return false end
    return player:getMark("pingrong_self-turn") == 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@pingrong_extra", 0)
  end,
})
pingrong:addEffect(fk.Damage, {
  mute=true,
  can_refresh = function(self, event, target, player, data)
    --FIXME:巨大隐患
    if player ~= target or player:getMark("@@pingrong_extra") == 0 then return false end
    return true
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@pingrong_extra", 0)
  end,
})

pingrong:addEffect(fk.EventPhaseStart, {
  anim_type = "negative",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player == target and player.phase == Player.Finish and
    player:getMark("@@pingrong_extra") ~= 0 and player:getMark("pingrong_self-turn") == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:loseHp(player, 1, self.name)
  end,
})

return pingrong